﻿package mortal.game.view.copy.treasureHunt
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import flash.events.*;
    import mortal.common.cd.*;
    import mortal.component.gconst.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.treasureHunt.view.*;
    import mortal.mvc.core.*;

    public class TreasureHuntController extends CopyControllerBase
    {
        private var _isTreasureHuntOpen:Boolean;
        private var _secTimerViewData:SecTimerViewData;
        private var _isInActivity:Boolean;

        public function TreasureHuntController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.TreasureHuntEnterCopy, this.onEnterTreasureHuntHandler);
            NetDispatcher.addCmdListener(ServerCommand.TreasureHuntCopyOpen, this.onTreasureHuntCopyOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.TreasureHuntCopyClose, this.onTreasureHuntCopyCloseHandler);
            NetDispatcher.addCmdListener(ServerCommand.TreasureHuntGetItem, this.onTreasureHuntGetItem);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onLevelUpdateHandler);
            Dispatcher.addEventListener(EventName.ShopMallOpenAndSelect, this.openTreasureEnterWin);
            return;
        }// end function

        private function onWinShow(event:Event) : void
        {
            return;
        }// end function

        private function onWinClose(event:Event) : void
        {
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            super.enterCopy(param1);
            AIManager.cancelAll();
            TreasureHuntEnterWinNew.instance.hide();
            TreasureHuntDetailWin.instance.show();
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrackExceptBtn, false));
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            TreasureHuntDetailWin.instance.hide();
            return;
        }// end function

        private function onEnterTreasureHuntHandler(event:DataEvent) : void
        {
            CopyUtil.copyCheck(this.enterTreasureHuntCopy);
            return;
        }// end function

        private function enterTreasureHuntCopy() : void
        {
            GameProxy.copy.enterTreasureHuntCopy();
            return;
        }// end function

        private function onTreasureHuntCopyOpenHandler(param1:Object) : void
        {
            this._isTreasureHuntOpen = true;
            this.onTreasureHuntCopyOpen(true);
            return;
        }// end function

        private function onTreasureHuntCopyOpen(param1:Boolean = false) : void
        {
            var _loc_4:Date = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_2:* = CopyConfig.instance.getCopyInfoByCode(GameConst.TreasureHuntCopyCode).enterMinLevel;
            if (cache.role.entityInfo.level < _loc_2)
            {
                return;
            }
            var _loc_3:* = cache.copy.treasureHunt.treasureHuntTimeInfo;
            if (_loc_3)
            {
                _loc_4 = ClockManager.instance.nowDate;
                _loc_5 = (_loc_3.startDt.time - _loc_4.time) * 0.001;
                _loc_6 = (_loc_3.endDt.time - _loc_4.time) * 0.001;
                TreasureHuntIcon.instance.show();
                if (_loc_5 > 0)
                {
                    this._isInActivity = false;
                    TreasureHuntIcon.instance.setLeftTimeAndState(_loc_5, 1);
                    if (!this._secTimerViewData)
                    {
                        this._secTimerViewData = new SecTimerViewData();
                        this._secTimerViewData.setLeftTime(_loc_5);
                        this._secTimerViewData.timeOutCaller = this.showEndLeftTime;
                    }
                }
                else
                {
                    this._isInActivity = true;
                    TreasureHuntIcon.instance.playEffect();
                    TreasureHuntIcon.instance.setLeftTimeAndState(_loc_6, 2);
                    if (param1 && !GameMapUtil.curMapState.isTreasureHunt)
                    {
                        TreasureHuntEnterWinNew.instance.show();
                    }
                }
            }
            return;
        }// end function

        private function showEndLeftTime() : void
        {
            var _loc_2:Date = null;
            var _loc_3:int = 0;
            if (this._secTimerViewData)
            {
                this._secTimerViewData.dispose();
                this._secTimerViewData = null;
            }
            TreasureHuntIcon.instance.playEffect();
            var _loc_1:* = cache.copy.treasureHunt.treasureHuntTimeInfo;
            if (_loc_1)
            {
                _loc_2 = ClockManager.instance.nowDate;
                _loc_3 = (_loc_1.endDt.time - _loc_2.time) * 0.001;
                TreasureHuntIcon.instance.setLeftTimeAndState(_loc_3, 2);
            }
            if (!GameMapUtil.curMapState.isTreasureHunt)
            {
                TreasureHuntEnterWinNew.instance.show();
            }
            this._isInActivity = true;
            return;
        }// end function

        private function onTreasureHuntCopyCloseHandler(param1:Object) : void
        {
            this._isTreasureHuntOpen = false;
            TreasureHuntIcon.instance.hide();
            if (TreasureHuntEnterWinNew.hasInstance() && !TreasureHuntEnterWinNew.instance.isHide)
            {
                TreasureHuntEnterWinNew.instance.hide();
            }
            return;
        }// end function

        private function onTreasureHuntGetItem(param1:Object) : void
        {
            var _loc_2:* = param1 as ItemData;
            if (GameMapUtil.curMapState.isTreasureHunt && _loc_2)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.FlyItemToPack, _loc_2));
            }
            return;
        }// end function

        private function onLevelUpdateHandler(param1:Object) : void
        {
            if (this._isTreasureHuntOpen)
            {
                this.onTreasureHuntCopyOpen();
            }
            return;
        }// end function

        private function activesNoticHandler(event:DataEvent) : void
        {
            var _loc_2:* = CopyConfig.instance.getCopyInfoByCode(GameConst.TreasureHuntCopyCode).enterMinLevel;
            if (cache.role.entityInfo.level < _loc_2)
            {
                return;
            }
            var _loc_3:* = event.data as SClientActive;
            if (_loc_3.type == EActiveType._EActiveTypeRaider)
            {
                TreasureHuntIcon.instance.show();
                TreasureHuntIcon.instance.setLeftTimeAndState((_loc_3.startDt.time - ClockManager.instance.nowDate.time) * 0.001, 1);
            }
            return;
        }// end function

        private function openTreasureEnterWin(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            if (_loc_2 == "show me the duobao")
            {
                TreasureHuntEnterWinNew.instance.show();
            }
            return;
        }// end function

        public function get isInActivity() : Boolean
        {
            return this._isInActivity;
        }// end function

    }
}
